package cn.edu.hit.core.message;

import cn.edu.hit.HttpRequestor;
import cn.edu.hit.HttpRequestorWithTokenForJoinsystem;
import cn.edu.hit.core.conf.ConfigureLoader;
import cn.edu.hit.kg.Neo4jConfigurationUtil;
import net.sf.json.JSONObject;
import cn.edu.hit.core.Document;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 获取钧安世通实时慢数据
 */
public final class JoinSystemAdapter extends MessageAdapterHelper2<JSONObject> {

    @Override
    public boolean accept(String type) {
        return "JoinSystem".equalsIgnoreCase(type);
    }

    private static HttpRequestorWithTokenForJoinsystem requestor;
    private String token_aquire_url;
    private String token_aquire_user;
    private String token_aquire_password;
    private String feature_aquire_url;


    private static String urlString = "http://172.16.45.250:8094/rsget/realtimeData/points/";

    private static long work_span = 10000;

    {
        Document conf = ConfigureLoader.getInstance().getBaseConfigure();
        urlString = conf.getString("JOINSYSTEM_GET_URL", "http://172.16.45.250:8094/rsget/realtimeData/points/");

        String WORK_SPAN = conf.getString("WORK_SPAN", "10000");

        token_aquire_url = conf.getString("token_aquire_url", token_aquire_url);
        token_aquire_user = conf.getString("token_aquire_user", token_aquire_user);
        token_aquire_password = conf.getString("token_aquire_password", token_aquire_password);
        feature_aquire_url = conf.getString("feature_aquire_url", feature_aquire_url);
        requestor = HttpRequestorWithTokenForJoinsystem.getInstance(token_aquire_url, token_aquire_user, token_aquire_password);

        try {
            work_span = Integer.parseInt(WORK_SPAN);
            if (work_span <= 500) {
                work_span = 500;
            }
        } catch (NumberFormatException ex) {
            System.err.println("配置文件不正确，" + WORK_SPAN + "应当为大于500的整数。 ");
        }
    }

    @Override
    protected JSONObject pretreat(Long timestamp) {
        List<String> jsIds = configureMap.entrySet().parallelStream().map(entry -> {
            Document configure = (Document) entry.getValue();
            return configure.getString(DATA_SOURCE_ARGS);
        }).collect(Collectors.toList());

        if (jsIds.size() == 0)
            return new JSONObject();

        try {
            return requestAsJson(jsIds, timestamp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JSONObject();
    }

    @Override
    protected Object getValue(JSONObject data, String id, Document configure) {
        String joinSystemId = configure.getString(DATA_SOURCE_ARGS);
        if (!data.containsKey(joinSystemId))
            return Collections.emptyList();
        String valueString = data.getString(joinSystemId);
        if ("-".equals(valueString)) {
            return Collections.emptyList();
        } else {
            return Collections.singletonList(Double.parseDouble(valueString));
        }
    }

    private static JSONObject requestAsJson(List<String> jsIds, Long timestamp) throws Exception {
        String url = urlString + String.join(",", jsIds);
//        String url = String.format(urlString + String.join(",",jsIds)+"/times?beginTime=%d&endTime=%d",timestamp-work_span,timestamp);

        System.out.println("get url" + url);
        String resultJson = requestor.doGet(url);
        System.out.println("输出返回数据" + resultJson);
        JSONObject jsonObject = JSONObject.fromObject(resultJson);
        return jsonObject.getJSONObject("data");
    }


}
